package com.cy8;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author CY
 * @date 2022/10/26 10:45
 **/
class Student {
    private String name;

    public Student(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}

public class MyHashBuck<K, V> {
    static class Node<K, V> {
        public K k;
        public V v;
        Node<K, V> next = null;

        public Node(K k, V v) {
            this.k = k;
            this.v = v;
        }

        public void Node(K k, V v) {
            this.k = k;
            this.v = v;
        }
    }

    public int usedSize;


    public Node<K, V>[] elem;

    public MyHashBuck() {
        elem = (Node<K, V>[]) new Node[4];
        this.usedSize = 0;
    }

    public void put(K k, V v) {
        int hash = k.hashCode();
        int index = hash % elem.length;
        Node cur = elem[index];
        while (cur != null) {
            if (cur.k.equals(k)) {
                cur.v = v;
                return;
            }
            cur = cur.next;
        }
        //头插法
        Node<K, V> node = new Node<K, V>(k, v);
        node.next = elem[index];
        elem[index] = node;
        usedSize++;
        //判断负载因子是否大于0.75
        if (loadFactor()) {
            resize();
        }
    }

    private void resize() {
        Node<K, V>[] newElem = Arrays.copyOf(elem, 2 * elem.length);
        for (int i = 0; i < elem.length; i++) {
            Node<K, V> old = elem[i];
            while (old != null) {
                int newIndex = old.hashCode() % newElem.length;
                Node<K, V> cur = newElem[newIndex];
                while (cur != null) {
                    if (cur.k.equals(old.k)) {
                        cur.v = old.v;
                        break;
                    }
                    cur = cur.next;
                }
                old = old.next;
            }
        }
        elem = newElem;
    }

    public V get(K k) {
        int index = k.hashCode() % elem.length;
        Node<K, V> cur = elem[index];
        while (cur != null) {
            if (cur.k.equals(k)) {
                return cur.v;
            }
        }
        return null;
    }

    private boolean loadFactor() {
        if (usedSize * 1.0f / elem.length >= 0.75) {
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        MyHashBuck<Student, Integer> hm = new MyHashBuck<>();
        hm.put( new Student("chenyong"),1);
//        hm.put(2, new Student("chenyon"));
//        hm.put(3, new Student("chenyo"));
//        hm.put(4, new Student("cheny"));
        System.out.println(hm.get(new Student("chenyong")));
//        System.out.println(hm.get(3));
//        System.out.println(hm.get(4));
//        System.out.println(hm.get(2));
        Integer.valueOf(128);



    }
}
